package com.hs.pre.db;

import java.io.File;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.list.TreeList;

import com.hs.Environment;
import com.hs.HsBase;
import com.hs.core.db.HsPreparedStatement;
import com.hs.core.db.HsResultSet;
import com.hs.log.HsLogger;
import com.hs.pre.model.PreEntry;
import com.hs.pre.util.PreParse;
import com.hs.utils.Utilities;

public class InitDatabaseData {
   private static final HsLogger logger = HsLogger.getLogger(InitDatabaseData.class.getName());
   //数据文件名称
   public static final String FILE_NAME = "part";
   public static String FILE_PATH = null; 
   public InitDatabaseData(){
	try {
		FILE_PATH = Utilities.getDbRoot().getParent() + System.getProperty("file.separator")+"data" +System.getProperty("file.separator");
	} catch (Exception e) { 
	}
   }
   /**
    * 读取方剂数据
    * @param fileName
    * @return
    */
   public List readData(String fileName){
	   TreeList data = new TreeList();
	   String filePath = this.FILE_PATH + fileName;
	   PreParse parser = new PreParse(new File(filePath));
	   List preList = parser.getDoc();
	   
	   if(null != preList){
		   logger.fine("开始读取方剂文件" + fileName);
		   for(int i=0;i<preList.size();i++){
			   Map m = (Map)preList.get(i);
			   PreEntry entry = new PreEntry(m);
			   entry.setSubmitFlag(PreEntry.getFlagByVersion());
			   data.add(entry); 
		   }
	   }
	   return data;
   }
   /**
    * 测试数据库中是否已经存在方剂数据
    * @return
    */
   public boolean testData(){
	   boolean flag = true;
	   try {
		HsPreparedStatement st = new HsPreparedStatement(HsBase.getAnsiSqlMap().get("preinfo.count")+"");
		   HsResultSet rs = st.executeQuery();
		   if(rs.next()){
			   int count = rs.getInt("count");
			   if(count == 0)
				    flag = false;
		   }
	} catch (SQLException e) { 
		e.printStackTrace();
	} catch (Exception e) { 
		e.printStackTrace();
	}
	   return flag;
   }
   /**
    * 删除数据
    */
   public void delData(String sql){ 
	   try {
			HsPreparedStatement st = new HsPreparedStatement(sql);
			int count = st.executeUpdate();
			logger.severe("删除数据" + count + "条!");
		} catch (SQLException e) { 
			e.printStackTrace();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
   }
   /**
    * 删除数据
    */
   public String insertPre(String sql,PreEntry entry){ 
	   String preId = null;
	   try {
			HsPreparedStatement st = new HsPreparedStatement(sql,true);
			String suoyinzt = PreEntry.INDEX_STATUS_ONE;//索引状态
		    String suoyinsj = "";//索引时间 
			st.setString(1, entry.getFangm());
			st.setString(2, entry.getBiem());
			st.setString(3, entry.getChufly());
			st.setString(4, entry.getYaowzc());
			st.setString(5, entry.getJiaj());
			st.setString(6, entry.getGongx());
			st.setString(7, entry.getZhuz());
			st.setString(8, entry.getId());
			st.setString(9, entry.getZhibff());
			st.setString(10, entry.getYongfyl());
			st.setString(11, entry.getYongyjj());
			st.setString(12, entry.getLincyy());
			st.setString(13, entry.getYaolzy());
			st.setString(14, entry.getGejls());
			st.setString(15, entry.getBeiz());
			st.setString(16, suoyinzt);
			st.setString(17, suoyinsj);
			st.setTimestamp(18, new Timestamp(System.currentTimeMillis()));
			st.setString(19, PreEntry.getFlagByVersion());
			preId = st.executeUpdateReturnId(); 
			logger.info("新增方剂[" + entry.getFangm()+"]");
		} catch (SQLException e) { 
			e.printStackTrace();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return preId;
   }
   
   public List initData(int fileNum,boolean isDelete){
	   List preList = null;
	   if(isDelete){
		   delData(HsBase.getAnsiSqlMap().get("preinfo.delete")+"");
	   }
	   if(!testData()){
		   
		   for(int i = 1;i <= fileNum;i++){
			  preList = readData(this.FILE_NAME+i+".xml"); 
		   }
	   }
	   return preList;
   }
   public static void main(String[] args){
	   
   }
}
